<?php
/**
 * 大话水浒 活动...奖励
 * Enter description here ...
 * @author Administrator
 *
 */
class Module_overlordaward{
	/**
	 * 构造函数
	 */
	function __construct() {
	
	}
	/**
	 * 礼包领取
	 * Enter description here ...
	 */
	function gift_bag(){
		$doPost = isset($_POST['doPost']) ? intval($_POST['doPost']) : 0;
		if(empty($doPost)) {
			return View::error('参数错误.');
		}

		$uin = sessionInfo('uin');
		$key = 'user_'.$uin;
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$result = App_Overlord_Query::oneUser($uin);
		if (empty($result)){
			return View::error('君主ID号不存在.');
		}
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		
		$end_time = $result['Value']['new']['time'] - $time;
		if ($end_time > 0) {
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlorduser');
			break;
		}
		
		//查询背包数据
		$items_info = App_Overlord_Query::oneItems($uin);
		if (empty($items_info)){
			$items_info['Value'] = array();
			App_Overlord_Query::addItems($uin, $items_info['Value']);
		}		
		//新手奖励
		$new_prop = App_Overlord_GameStart::getNewUser($result['Value']['new']['num']);
		if (!empty($new_prop)){
			foreach ($new_prop['prop'] as $k => $row){
				if (!empty($items_info['Value'][$k][$row[0]])){
					$items_info['Value'][$k][$row[0]]['count'] += $row[1];
				}else{
					$items_info['Value'][$k][$row[0]]['count'] = $row[1];
				}
			}		
		}
		//修改背包
		App_Overlord_Query::alterItems($uin, $items_info['Value']);
		$num = $result['Value']['new']['num']+1;
		$new = App_Overlord_GameStart::getNewUser($num);  
		$result['Value']['new']['num'] = $num;
		$result['Value']['new']['time'] = $new['time'] * 60 + $time;
		
		if ($num > 4){
			$result['Value']['new'] = '';
			$num = 5;
		}
		//修改君主信息
		App_Overlord_Query::alterUser($uin, $result['Value']);
		App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type=new&id='.$num);
	}
/**
	 * 君主等级礼包
	 */
	function award_pege(){
		$uin = SessionInfo('uin');
		$smarty = loadSmarty();
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Collocate');
		loadLib('App_Overlord_Query');
		$list = App_Overlord_Query::oneUser($uin);
		if (empty($list)){
			return View::error('获取君主资料失败.');
		}
		//查询配置物品信息
		$data = App_Overlord_Collocate::getGoods();
		//获取等级奖励配置
		$award = App_Overlord_GameStart::getLvAward($list['Value']['gift_lv']);
		if (!empty($award['award'])){
			foreach ($award['award'] as $k => $row){
				if ($k == 'gold'){
					$award_list[$k]['name'] = '黄金';
					$award_list[$k]['count'] = $row;
				}else{
					$award_list[$k]['name'] = $data[$k][$row[0]]['name'];
					$award_list[$k]['count'] = $row[1];
				}
			}
		}
		if (!empty($award_list)){
			$smarty->assign('award_list',$award_list);
		}
		$smarty->assign('list',$list['Value']);
		return $smarty->fetch('app/Overlord/award_pege.tpl.php');
	}
	/**
	 * 君主等级礼包领取
	 */
	function award_lv(){
		$doPost = isset($_POST['doPost']) ? intval($_POST['doPost']) : 0;
		if(empty($doPost)) {
			return View::error('参数错误.');
		}

		$uin = SessionInfo('uin');
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		//君主资料
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('获取君主资料失败.');
		}
		if ($user_info['Value']['lv'] < $user_info['Value']['gift_lv']){
			return View::error('你的等级不够,此礼包需要' . $user_info['Value']['gift_lv'] .'级,赶快升级吧!');
		}
		//查询背包数据
		$items_info = App_Overlord_Query::oneItems($uin);
		if (empty($items_info)){
			$items_info['Value'] = array();
			$code = App_Overlord_Query::addItems($uin, $items_info['Value']);
		}
				
		//升级奖励
		$award = App_Overlord_GameStart::getLvAward($user_info['Value']['gift_lv']);
		if (!empty($award['award'])){
			foreach ($award['award'] as $k => $row){
				if ($k == 'gold'){
					$user_info['Value']['gold'] += $row;
				}
				if (!empty($items_info['Value'][$k][$row[0]])){
					$items_info['Value'][$k][$row[0]]['count'] += $row[1];
				}else{
					$items_info['Value'][$k][$row[0]]['count'] = $row[1];
				}
			}		
		}
		$user_info['Value']['gift_lv'] = $award['next'];
		//修改背包
		$code = App_Overlord_Query::alterItems($uin, $items_info['Value']);
		//修改君主
		$code = App_Overlord_Query::alterUser($uin, $user_info['Value']);
		App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type=level&id=100');
	}
	/**
	 * 公告
	 */
	function notice(){
		$smarty = loadSmarty();
		return $smarty->fetch('app/Overlord/notice.tpl.php');
	}
	/**
	 * 活动
	 */
	function exercise(){
		$smarty = loadSmarty();
		
		return $smarty->fetch('app/Overlord/exercise.tpl.php');
	}
	
	/**
	 * 连续登录活动
	 */
	function login_fate(){
		$smarty = loadSmarty();
		$uin = SessionInfo('uin');
		$day = array(1 , 2 , 3 , 4 , 5 , 6 ,7);
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Collocate');
		loadLib('App_Overlord_Query');
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('获取君主资料失败!');
		}
		//获取物品表
		$data_info = App_Overlord_Collocate::getGoods();
		//获取连续奖励
		foreach ($day as $k => $row){
			$data = App_Overlord_GameStart::getLoginUser($row);
			foreach ($data['award'] as $j => $rs){
				if ($j == 'gold'){
					$list[$row] = array('name' => '黄金'.$rs);
				}else{
					$list[$row] = array('name' => $data_info[$j][$rs[0]]['name'].'x'.$rs[1]);
				}
			}
		}
		$smarty->assign('user_info',$user_info['Value']);
		$smarty->assign('day',$list);
		return $smarty->fetch('app/Overlord/login_fate.tpl.php');
	}
	/**
	 * 奖励说明
	 */
	function login_award(){
		$day = isset($_GET['day']) ? $_GET['day'] : '';
		$uin = SessionInfo('uin');
		$smarty = loadSmarty();
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Collocate');
		loadLib('App_Overlord_Query');
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('获取君主资料失败!');
		}
		
		//获取连续奖励
		$data = App_Overlord_GameStart::getLoginUser($day);
		//获取物品表
		$data_info = App_Overlord_Collocate::getGoods();
		if (empty($data)){
			return View::error('没有该项奖励!');
		}
		foreach ($data['award'] as $k => $row){
			if ($k == 'gold'){
				$list[] = array('name' => '黄金' , 'count' => $row);
			}else{
				$list[] = array('name' => $data_info[$k][$row[0]]['name'] , 'count' => $row[1]);
			}
		}
		$smarty->assign('user_info',$user_info['Value']);
		$smarty->assign('list',$list);
		$smarty->assign('day',$day);
		return $smarty->fetch('app/Overlord/login_award.tpl.php');
	}
	/**
	 * 领取连续登陆奖励
	 */
	function login_draw(){
		$day = isset($_POST['day']) ? $_POST['day'] : '';
		$uin = SessionInfo('uin');
		$smarty = loadSmarty();
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Collocate');
		loadLib('App_Overlord_Query');
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('获取君主资料失败!');
		}
		if ($day < $user_info['Value']['login_fate']['count'] || !empty($user_info['Value']['login_fate']['state'])){
			return View::error('你已领取此礼包!');
		}
		//获取连续奖励
		$data = App_Overlord_GameStart::getLoginUser($day);
		if (empty($data)){
			return View::error('没有该项奖励!');
		}
		//查询背包数据
		$items_info = App_Overlord_Query::oneItems($uin);
		if (empty($items_info)){
			$items_info['Value'] = array();
			$code = App_Overlord_Query::addItems($uin, $items_info['Value']);
		}	
		foreach ($data['award'] as $k => $row){
			if ($k == 'gold'){
				//黄金
				$user_info['Value']['gold'] += $row;
			}else{
				if (!empty($items_info['Value'][$k][$row[0]])){
					$items_info['Value'][$k][$row[0]]['count'] += $row[1];
				}else{
					$items_info['Value'][$k][$row[0]]['count'] = $row[1];
				}
				
			}
		}
		//修改领取状态
		$user_info['Value']['login_fate']['state'] = 1;
		//修改背包
		$code = App_Overlord_Query::alterItems($uin, $items_info['Value']);
		//修改背包
		$code = App_Overlord_Query::alterUser($uin, $user_info['Value']);
		App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordaward&a=login_draw_pege&day='.$day);
	}
	/**
	 * 领取成功显示
	 */
	function login_draw_pege(){
		$day = isset($_GET['day']) ? $_GET['day'] : '';
		$smarty = loadSmarty();
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Collocate');
		if ($day >6){
			$nextday = 1;
		}else{
			$nextday = $day + 1;
		}
		//获取连续奖励
		$data = App_Overlord_GameStart::getLoginUser($day);
		
		//获取物品表
		$data_info = App_Overlord_Collocate::getGoods();
		if (empty($data)){
			return View::error('没有该项奖励!');
		}
		//获取下次登录
		$nextdata = App_Overlord_GameStart::getLoginUser($nextday);
		foreach ($data['award'] as $k => $row){
			if ($k == 'gold'){
				$list[] = array('name' => '黄金' , 'count' => $row);
			}else{
				$list[] = array('name' => $data_info[$k][$row[0]]['name'] , 'count' => $row[1]);
			}
		}
		foreach ($nextdata['award'] as $k => $row){
			if ($k == 'gold'){
				$nextdata_info[] = array('name' => '黄金' , 'count' => $row);
			}else{
				$nextdata_info[] = array('name' => $data_info[$k][$row[0]]['name'] , 'count' => $row[1]);
			}
		}
		
		$smarty->assign('list',$list);
		$smarty->assign('nextdata_info',$nextdata_info);
		$smarty->assign('day',$day);
		return $smarty->fetch('app/Overlord/login_draw_pege.tpl.php');
	}
}
# end of script